const Mock = require('mockjs')
const Random = Mock.Random

let Result = {
  code: 200,
  msg: 'success',
  data: null
}

// 模拟场馆数据
let venueInfo = [
  {
    id: 1,
    name: '国家体育场（鸟巢）',
    address: '北京市朝阳区国家体育场南路1号',
    capacity: 91000,
    state: '空闲'
  },
  {
    id: 2,
    name: '上海体育场',
    address: '上海市徐汇区天钥桥路666号',
    capacity: 56000,
    state: '空闲'
  },
  {
    id: 3,
    name: '广州天河体育中心',
    address: '广州市天河区天河路299号',
    capacity: 60000,
    state: '筹备中'
  },
  {
    id: 4,
    name: '深圳大运中心',
    address: '深圳市龙岗区龙翔大道3001号',
    capacity: 60000,
    state: '维护中'
  },
  {
    id: 5,
    name: '重庆奥林匹克体育中心',
    address: '重庆市九龙坡区科城路68号',
    capacity: 60000,
    state: '已废弃'
  }
]

// 模拟预约记录
let reservationHistory = [
  {
    id: 1,
    venueName: '国家体育场（鸟巢）',
    reservationDate: '2025-06-25',
    userId: '123',
    status: '审核中'
  },
  {
    id: 2,
    venueName: '上海体育场',
    reservationDate: '2025-06-28',
    userId: '123',
    status: '已接受'
  },
  {
    id: 3,
    venueName: '国家体育场（鸟巢）',
    reservationDate: '2025-06-30',
    userId: 'user2',
    status: '已拒绝'
  }
]

// 模拟显示场馆信息
// Mock.mock('/getVenueInfo', 'get', () => {
//   Result.data = venueInfo
//   return Result
// })

// 模拟提交预约
// Mock.mock('/submitReservation', 'post', (options) => {
//   Result = { code: 200, msg: 'success', data: null } // 重置状态
//   const body = JSON.parse(options.body)
//   const venue = venueInfo.find(v => v.name === body.venueName)
//   if (!venue) {
//     Result.code = 400
//     Result.msg = '场馆名称不正确'
//     return Result
//   }
//   if (venue.state !== '空闲') {
//     Result.code = 400
//     Result.msg = '该场馆当前状态不可预约'
//     return Result
//   }
//   const existingReservation = reservationHistory.some(r => r.venueName === body.venueName && r.reservationDate === body.reservationDate)
//   if (existingReservation) {
//     Result.code = 400
//     Result.msg = '该场馆该日已有预约'
//     return Result
//   }
//   const newId = Math.max(...reservationHistory.map(r => r.id)) + 1 || 1
//   const newReservation = {
//     id: newId,
//     ...body,
//     status: '审核中'
//   }
//   reservationHistory.push(newReservation)
//   Result.msg = '预约提交成功'
//
//   if (existingReservation) {
//     Result.code = 400
//     Result.msg = `预约失败：${body.venueName}在${body.reservationDate}已有预约`
//     return Result
//   }
//   return Result
// })

// 模拟获取过往预约记录
// Mock.mock(/\/getReservationHistory/, 'get', (options) => {
//   const userId = new URLSearchParams(options.url.split('?')[1]).get('userId')
//   Result.data = reservationHistory.filter(r => r.userId === userId)
//   return Result
// })

// 模拟删除预约
// Mock.mock(new RegExp('/deleteReservation/\\d+'), 'delete', (options) => {
//   const id = parseInt(options.url.match(/\/deleteReservation\/(\d+)/)[1])
//   const index = reservationHistory.findIndex(r => r.id === id)
//   if (index !== -1) {
//     reservationHistory.splice(index, 1)
//     Result.msg = '删除预约成功'
//   } else {
//     Result.code = 404
//     Result.msg = '未找到该预约记录'
//   }
//   return Result
// })

// 新增修改预约接口
// Mock.mock(new RegExp('/updateReservation/\\d+'), 'put', (options) => {
//   const id = parseInt(options.url.match(/\/updateReservation\/(\d+)/)[1])
//   const body = JSON.parse(options.body)
//   const index = reservationHistory.findIndex(r => r.id === id)
//
//   if (index === -1) {
//     Result.code = 404
//     Result.msg = '未找到该预约记录'
//     return Result
//   }
//
//   // 验证场馆状态
//   const venue = venueInfo.find(v => v.name === body.venueName)
//   if (!venue) {
//     Result.code = 400
//     Result.msg = '场馆不存在'
//     return Result
//   }
//
//   // 验证日期冲突
//   const conflict = reservationHistory.some(r =>
//     r.id !== id &&
//     r.venueName === body.venueName &&
//     r.reservationDate === body.reservationDate
//   )
//
//   if (conflict) {
//     Result.code = 400
//     Result.msg = '该场馆该日已有预约'
//     return Result
//   }
//
//   // 更新数据
//   reservationHistory[index] = {
//     ...reservationHistory[index],
//     ...body,
//     status: '审核中' // 修改后重置状态
//   }
//
//   Result.msg = '修改成功'
//   return Result
// })

// 新增预约总览接口
// Mock.mock('/getReservationOverview', 'get', () => {
//   // 生成示例数据
//   const mockData = Array.from({ length: 15 }, (_, i) => ({
//     id: i + 4,
//     venueName: Random.pick(['国家体育场（鸟巢）', '上海体育场', '广州天河体育中心']),
//     reservationDate: Random.date('yyyy-MM-dd'),
//     userName: Random.name(),
//     status: Random.pick(['审核中', '已接受', '已拒绝'])
//   }))
//
//   return {
//     ...Result,
//     data: [...reservationHistory, ...mockData]
//   }
// })

// 新增状态修改接口
// Mock.mock(new RegExp('/updateReservationStatus/\\d+'), 'put', (options) => {
//   const id = parseInt(options.url.match(/\/updateReservationStatus\/(\d+)/)[1])
//   const body = JSON.parse(options.body)
//   const index = reservationHistory.findIndex(r => r.id === id)
//
//   if (index !== -1) {
//     reservationHistory[index].status = body.status
//     return { ...Result, msg: '状态更新成功' }
//   }
//   return { ...Result, code: 404, msg: '未找到该预约记录' }
// })

module.exports = Mock